package com.neusoft.chenanqi.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.chenanqi.dao.EmpSalDao;
import com.neusoft.chenanqi.dto.EmpSalDto;
import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.basic.model.Pager;
import com.zero.mms.model.Dep;
import com.zero.mms.model.EmpSalary;
import com.zero.mms.model.Employee;
import com.zero.mms.model.SalaryItem;

@Service("fixedSalService")
@Transactional(rollbackFor = Exception.class)
public class FixedSalServiceImpl implements FixedSalService {

	@Inject
	private EmpSalDao empSalDao;
	
	// 根据条件查询固定工资列表
	@Override
	public Pager<EmpSalDto> listFixedSal(String depName, String empNo,
			String startMonth, String endMonth, String salFrom, String salTo)
			throws SmsRuntimeException {
		try {
			if (depName == null) {
				depName = "*";
			}
			if (empNo == null) {
				empNo = "*";
			}
			if (startMonth == null) {
				startMonth = "*";
			}
			if (endMonth == null) {
				endMonth = "*";
			}
			
			//判断是否有固定工资范围条件
			Double minSal = null;
			Double maxSal = null;
			if (!"*".equals(salFrom) && !"".equals(salFrom) && salFrom != null) {
				minSal = Double.parseDouble(salFrom);
			}
			if (!"*".equals(salTo) && !"".equals(salTo) && salTo != null) {
				maxSal = Double.parseDouble(salTo);
			}
			
			// 获得固定工资记录的人员月份信息
			Pager<EmpSalDto> empSalDtoPager = empSalDao.listEmpSal(depName, empNo, startMonth, endMonth, minSal, maxSal, "固定");
			
			// 获取固定工资项目名称列表
			List<SalaryItem> salaryItemList = empSalDao.listSalItemByType("固定");
			List<String> fixedSalaryItemName = new ArrayList<String>();
			for (SalaryItem salaryItem : salaryItemList) {
				fixedSalaryItemName.add(salaryItem.getSalaryItemName());
			}
			
			// 获得每条固定工资记录的所有固定工资项目值
			List<EmpSalDto> empSalDtoList = empSalDtoPager.getDatas();
			for (EmpSalDto empSalDto : empSalDtoList) {
				empSalDto.setSalItemName(fixedSalaryItemName);
				for (String fixedSalName : fixedSalaryItemName) {
					EmpSalary empSalary = empSalDao.getEmpSalByEmpNoMonthAndItemName(empSalDto.getEmpNo().toString(), 
							empSalDto.getMonth(), fixedSalName);
					
					if (empSalary == null || empSalary.getEmpSalaryNum() == null || "".equals(empSalary.getEmpSalaryNum())) {
						empSalDto.addsalItemValue("0");
					} else {
						empSalDto.addsalItemValue(empSalary.getEmpSalaryNum());
					}
				}
			}
			
			return empSalDtoPager;
		} catch (NumberFormatException e)  {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,
					SmsErrorConstant.INVALID_AUGUMENT, "工资范围输入错误");
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "查询固定工资列表失败");
		}
	}

	// 更新固定工资信息
	@Override
	public void updateFixedSal(String empNo, String month,
			String[] fixedSalName, String[] fixedSalValue)
			throws SmsRuntimeException {
		try {
			for (int i = 0; i < fixedSalName.length; i++) {
				if (!"*".equals(fixedSalValue[i]) && !"".equals(fixedSalValue[i])) {
					SalaryItem salaryItem = empSalDao.getSalItemByNameAndType(fixedSalName[i], "固定");
					if (salaryItem == null) {
						throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,
								SmsErrorConstant.INVALID_AUGUMENT, "存在未知的固定工资项目");
					}
					EmpSalary empSalary = empSalDao.getEmpSalByEmpNoMonthAndItemName(empNo, month, fixedSalName[i]);
					if (empSalary == null) {
						empSalary = new EmpSalary();
						Integer empId = Integer.parseInt(empNo);
						empSalary.setEmpId(empId);
						empSalary.setEmpSalaryDate(month);
						empSalary.setSalaryItemId(salaryItem.getSalaryItemId().toString());
						empSalary.setEmpSalaryNum(fixedSalValue[i]);
						empSalDao.add(empSalary);
					} else {
						if (empSalary.getEmpSalaryState() != null && 
								"已发".equals(empSalary.getEmpSalaryState())) {
							throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,
									SmsErrorConstant.INVALID_AUGUMENT, "不能更改已发放的工资项目");
						}
						empSalary.setEmpSalaryNum(fixedSalValue[i]);
						empSalDao.update(empSalary);
					}
				}
			}
		} catch (SmsRuntimeException e) {
			e.printStackTrace();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "更新固定工资信息失败");
		}
	}

	// 查询部门列表
	@Override
	public List<Dep> listDep() throws SmsRuntimeException {
		try {
			List<Dep> depList = empSalDao.listDep();
			return depList;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "获取部门列表失败");
		}
	}

	// 根据部门ID查询员工列表
	@Override
	public List<Employee> listEmp(String depId) throws SmsRuntimeException {
		try {
			Integer id = Integer.parseInt(depId);
			List<Employee> empList = empSalDao.listEmpByDepId(id);
			return empList;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "获取员工列表失败");
		}
	}

	// 添加员工的固定工资信息
	@Override
	public void addEmpFixedSal(String empId, String startMonth,
			String endMonth, String[] fixedSalName, String[] fixedSalValue)
			throws SmsRuntimeException {
		try {
			if (startMonth.compareTo(endMonth) > 0) {
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE, 
						SmsErrorConstant.INVALID_AUGUMENT, "月份范围不合法");
			} 
			Integer year1 = Integer.parseInt(startMonth.substring(0, 4));
			Integer month1 = Integer.parseInt(startMonth.substring(5, 7));
			Integer year2 = Integer.parseInt(endMonth.substring(0, 4));
			Integer month2 = Integer.parseInt(endMonth.substring(5, 7));
			
			Calendar start = Calendar.getInstance();
	        start.set(Calendar.YEAR, year1);
	        start.set(Calendar.MONTH, month1 - 1);
	        Calendar end = Calendar.getInstance();
	        end.set(Calendar.YEAR, year2);
	        end.set(Calendar.MONTH, month2 - 1);
	        
	        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
	        
	        while (start.compareTo(end) <= 0) {
	        	String month = format.format(start.getTime());
	        	updateFixedSal(empId, month, fixedSalName, fixedSalValue);
	        	start.add(Calendar.MONTH, 1);
	        }
		} catch (SmsRuntimeException e) {
			e.printStackTrace();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "添加员工的固定工资信息失败");
		}
		
	}

	// 批量录入固定工资信息
	@Override
	public void importFixedSal(String startMonth, String endMonth,
			List<EmpSalDto> empSalDtoList) throws SmsRuntimeException {
		try {
			for (EmpSalDto empSalDto : empSalDtoList) {
				addEmpFixedSal(empSalDto.getEmpNo().toString(), startMonth, 
						endMonth, empSalDto.getSalItemName().toArray(new String[0]), 
						empSalDto.getSalItemValue().toArray(new String[0]));
			}
		} catch (SmsRuntimeException e) {
			e.printStackTrace();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "批量录入固定工资信息失败");
		}
	}

}
